package com.flyou.ccnucenter.ui.activity.webView;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyou.ccnucenter.Constant;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;


public class WebViewActivity extends BaseActivity1 implements View.OnClickListener {

    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    private WebView mWebView;
    private RelativeLayout titlebar_rl_Right;
    private ImageView iv_right_image;
    @Override
    protected void initView() {
        setContentView(R.layout.activity_web_view);
        titlebar_rl_Right = (RelativeLayout) findViewById(R.id.titlebar_rl_Right);
        iv_right_image = (ImageView) findViewById(R.id.iv_right_image);
        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);
        mWebView = (WebView) findViewById(R.id.webview);
        mProgressDialog= ProgressDialog.show(WebViewActivity.this,null,"正在加载数据",false,true);
    }

    @Override
    protected void initDate() {
        String title = getIntent().getStringExtra("title");
        String url = getIntent().getStringExtra("url");
        if (TextUtils.isEmpty(title)) {
            return;
        }
        if (TextUtils.isEmpty(url)) {
            url = Constant.BASE_URL + Constant.ABOUT_ME;
        }

        if (title.equals("关于作者")) {
            titlebar_rl_Right.setVisibility(View.VISIBLE);
            iv_right_image.setImageResource(R.drawable.ic_action_like);
        }
        mTitlebarTvTitle.setText(title);


        WebSettings webSettings = mWebView.getSettings();
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setBlockNetworkImage(false);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setTextSize(WebSettings.TextSize.NORMAL);
        webSettings.setDomStorageEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if( url.startsWith("http:") || url.startsWith("https:") ) {
                    return false;
                }

                // Otherwise allow the OS to handle things like tel, mailto,sms , etc.
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity( intent );
                return true;
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                if (mProgressDialog.isShowing())
                mProgressDialog.dismiss();
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                if (mProgressDialog.isShowing())
                mProgressDialog.dismiss();

                Toast.makeText(WebViewActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });


        mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        mWebView.loadUrl(url);
    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
        titlebar_rl_Right.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        mWebView.destroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_rl_back:
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                } else {
                    finish();
                }
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }

    }
}
